<template>
  <var-app-bar
    title="ASG解说选班系统v2.0.0"
    color="#0089f3"
    title-position="center"
  />
  <!-- 表单 -->
  <div class="form-parent">
    <div class="title">
      <h2>系统登录</h2>
    </div>
    <div class="form-wrap">
      <var-form ref="form" scroll-to-error="start">
        <var-input
          placeholder="请输入ASG官网账号"
          :rules="[(v) => Boolean(v) || '用户名不能为空']"
          v-model="formData.username"
        />
        <var-input
          type="password"
          placeholder="请输入密码"
          :rules="[(v) => Boolean(v) || '密码不能为空']"
          v-model="formData.password"
        />
      </var-form>
      <div class="btn-wrap">
        <var-button
          color="#0089f3"
          :loading="loading"
          text-color="#fdfdfd"
          block
          type="primary"
          loading-type="cube"
          @click.native="handleSubmit(form)"
        >
          登录
        </var-button>
      </div>
      <div class="enroll-wrap"><a href="https://idvasg.cn" target="_blank">没有账号?点我注册</a></div>
    </div>
  </div>
  <var-app-bar title-position="center" color="#0089f3">
    <template #default>
      <div class="footer">
        <p>© 2023 - 2024 Avengers Gaming 版权所有</p>
        <p>ICP证：<a href="https://beian.miit.gov.cn/" target="_blank">鄂ICP备2024037646号-1</a></p>
      </div>
    </template>
  </var-app-bar>
</template>

<script setup lang="ts">
// 内部配置对象
import type{ FormValidation } from "@varlet/ui";
const router = useRouter();

const form = ref<FormValidation | null>(null);
let loading = ref(false);

let formData = reactive({
  username: "",
  password: "",
});
function handleSubmit(valid:FormValidation | null) {
  if(valid === null){
    return
  }
  loading.value = true;
  valid.validate().then((res) => {
    if (res) {
      loginByName(formData.username, formData.password)
        .then((res) => {
            setToken(res.data)
             getUserInfo()
             .then((res) => {
              // 保存权限：管理员：2，解说：1，游客：0
              const { roles,officium } = res.data 
              if(roles?.length!==0){
                sessionStorage.setItem("authorization", "2")
              }else if(officium === 'Commentator'){
                sessionStorage.setItem("authorization", "1")
              }else{
                sessionStorage.setItem("authorization", "0")
              }
              setTimeout(() => {
               loading.value = false;
               if(sessionStorage.getItem("authorization")==='0'){
                router.push({ path: "/home/enroll" });
               }else{
                router.push({ path: "/home/list" });
               }
               Snackbar.success("欢迎进入ASG选班系统！");
              }, 500);
             })
        })
        .catch((err) => {
          Snackbar.error(err.response.data.message);
          loading.value = false;
        });
    } else {
      Snackbar.error(`请完整填写表单内容`);
      loading.value = false;
    }
  });
}
</script>

<style scoped lang="less">
.enroll-wrap{
  margin:24px auto;
  text-align: center;
  a{
    font-size: 0.8rem;
  }
}
.form-parent {
  height: calc(100dvh - 54px - 54px);
  display: flex;
  flex-direction: column;
  justify-content: center;
  background-color: #fff;
}
.form-wrap {
  padding: 0 25px;
  :deep(.var-input) {
    margin-bottom: 20px;
  }
  .btn-wrap {
    margin-top: 50px;
  }
}
.title {
  display: flex;
  justify-content: center;
  align-items: center;
}
h2 {
  position: relative;
  margin: 0;
  font-size: 1.5em;
  font-weight: 700;
  color: @lightColor;
  z-index: 1;
  padding-bottom: 3px;
  margin-bottom: 20px;
  border-bottom: 3px solid @lightColor;
}
.footer{
  display:flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  p,a{
    font-size: 0.7rem;
    color:#dff

  }
  a{
    text-decoration: none;
  }
}
</style>
